<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>数据库操作</title>
<style type="text/css">
h1{ color:red;}
</style>
</head>
<body>
<h1>数据库操作</h1>
<pre>
(1)创建数据库
(2)选择要操作的数据库
(3)创建表
(4)删除记录
(5)向表中插入记录
(6)修改记录
(7)查询记录
</pre><%!
public void showUsers(Statement st,JspWriter out){
	try(ResultSet rs=st.executeQuery("select * from users")){
		while(rs.next()){
			String name=rs.getString("xm");
			int age=rs.getInt("age");
			out.println("<p>   "+name+",   "+age+"</p>");
	}
 }catch(Exception e){
	 
 }
}
%>
<% 
Class.forName("com.mysql.cj.jdbc.Driver");
try(Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "12qwas");
		Statement st = cn.createStatement()) {
	st.executeUpdate("create database if not exists db666");
	out.println("<p>成功创建数据库~</p>");
	st.executeUpdate("use db666");
	out.println("<p>成功切换数据库~</p>");
	st.executeUpdate("create table if not exists users( xm varchar(4), age int)");
	out.println("<p>成功创建数据表~</p>");
	int count=st.executeUpdate("delete from users");
	out.println("<p>成功删除"+count+"条记录~</p>");
	count=st.executeUpdate("insert into users( xm,age) values('张三',18),('李四',20)");
	out.println("<p>成功插入"+count+"条记录,记录如下: </p>");
	try(ResultSet rs=st.executeQuery("select * from users")){
		while(rs.next()){
			String name=rs.getString("xm");
			int age=rs.getInt("age");
			out.println("<p>   "+name+",   "+age+"</p>");
			
		}
	}
	count=st.executeUpdate("update users set xm='张三丰', age=81 where xm='张三' ");
	out.println("<p>成功修改"+count+"条记录,记录如下: </p>");
	try(ResultSet rs=st.executeQuery("select * from users")){
		while(rs.next()){
			String name=rs.getString("xm");
			int age=rs.getInt("age");
			out.println("<p>   "+name+",   "+age+"</p>");
			
	}
}
}

%>
</body>
</html>